package com.yycx.module.user.provider.service.impl;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.DateUtil;
import com.yycx.module.user.client.entity.SysFeedback;
import com.yycx.module.user.provider.mapper.SysFeedbackMapper;
import com.yycx.module.user.provider.service.SysFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 反馈意见 实现类
 *
 * @author flyme
 * @date 2019-09-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysFeedbackServiceImpl extends BaseServiceImpl<SysFeedbackMapper, SysFeedback> implements SysFeedbackService {

    @Override
    public ResultBody beforePageList(CriteriaQuery<SysFeedback> cq, SysFeedback sysFeedback, EntityMap requestMap) {
        SysFeedback query = cq.getEntity(SysFeedback.class);
        if (FlymeUtils.isNotEmpty(query)) {
            cq.lambda()
                    .like(FlymeUtils.isNotEmpty(query.getFeedContent()), SysFeedback::getFeedContent, query.getFeedContent())
                    .like(FlymeUtils.isNotEmpty(query.getMobile()), SysFeedback::getMobile, query.getMobile())
                    .eq(FlymeUtils.isNotEmpty(query.getFeedState()), SysFeedback::getFeedState, query.getFeedState());
        }
        cq.eq("user.userNo", requestMap.get("userNo"));
        cq.eq("user.userType", requestMap.get("userType"));
        cq.select("user.nickName", "user.userName", "user.avatar", "user.userNo", "user.userType");
        cq.createJoin("com.yycx.module.user.client.entity.AppUser");
        cq.orderByAsc("feedState");
        cq.orderByDesc("createTime");
        return ResultBody.ok();
    }


    @Override
    public ResultBody beforeAdd(CriteriaSave cs, SysFeedback sysFeedback, EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        sysFeedback.setUserId(userId);
        sysFeedback.setFeedState(CommonConstants.DISABLED);
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeEdit(CriteriaUpdate<SysFeedback> cu, SysFeedback sysFeedback, EntityMap extra) {
        String replyContent = sysFeedback.getReplyContent();
        if (FlymeUtils.isNotEmpty(replyContent)) {
            sysFeedback.setFeedState(1);
            sysFeedback.setReplyTime(DateUtil.now());
        }else{
            sysFeedback.setFeedState(0);
            sysFeedback.setReplyTime(null);
        }
        return super.beforeEdit(cu, sysFeedback, extra);
    }
}
